package com.wonderland.webtest.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gaoxy on 2015-09-15 0015.
 */
@Service
public class RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    public void test(){
        redisTemplate.execute(new RedisTemplate.JedisActionNoResult() {
            @Override
            public void action(Jedis jedis) {
                mapTest(jedis);
            }
        });
    }

    public void mapTest(Jedis jedis){
        Map<String,String> testMap = new HashMap<>();
        testMap.put("a","1");
        testMap.put("b","2");
        testMap.put("c", "3");
        jedis.hmset("testMap", testMap);
        System.out.println(jedis.hget("testMap","a"));
    }

    public void exec(Jedis jedis) {
        Date a = new Date();
        int i1 = 10000;
//        for (int i = 0; i < i1; i++) {
//            Jedis jedis = new Jedis("localhost");
//            jedis.set("w3ckey", "Redis tutorial");
//        }
        Date b = new Date();
//        System.out.println("time1:" + (b.getTime() - a.getTime()));
        Date c = new Date();
//        for (int i = 0; i < i1; i++) {
//            Jedis jedis = new Jedis("localhost");
//            jedis.get("w3ckey");
//        }
        Date d = new Date();
//        System.out.println("time2:" + (d.getTime() - c.getTime()));

        a = new Date();
        for (int i = 0; i < i1; i++) {
            jedis.set("w3ckey"+i, "Redis tutorial");
        }
        b = new Date();
        System.out.println("time3:" + (b.getTime() - a.getTime()));
        c = new Date();
        for (int i = 0; i < i1; i++) {
            jedis.get("w3ckey"+i);
        }
        d = new Date();
        System.out.println("time4:" + (d.getTime() - c.getTime()));
    }
}
